import pygui
from pygui import *

class wimgbutton(window):
	def __init__(self, name, image, function=None, function2=None, rect=None, z=0):
		window.__init__(self, name, rect, z)
		self.function = function
		self.function2 = function
		self.setImage(image)
		self.over = 0
		self.click = 0
		self.disabled = 0
		
	def setImage(self, image):
		if type(image) == type('') or type(image) == type(u''):
			self.surf = pygame.image.load(image).convert_alpha()
		else:
			self.surf = image
		
		size = self.surf.get_size()
		if size[0] > size[1]:
			self.offs = (size[0] / 4, 0)
			self.rect.size = (size[0] / 4, size[1])
		else:
			self.offs = (0, size[1] / 4)
			self.rect.size = (size[0], size[1] / 4)
		
		self.realign()
		
	def down(self, pos, button):	
		if self.disabled >= 1:
			return
		if self.rect.collidepoint(pos[0], pos[1]) == 1:
			if self.function is not None:
				self.function(self, pos, button)
			self.click = 1

				
	def up(self, pos, button):
		self.click = 0
		if self.disabled >= 1:
			return
		if self.function2 is not None and self.rect.collidepoint(pos[0], pos[1]) == 1 and self.click == 1:
			self.function2(self, pos, button)
			return
				
	def move(self, pos, rel, button):
		if self.disabled >= 1:
			return
		self.over = self.rect.collidepoint(pos[0], pos[1])
		
	def render(self, screen, offs=(0,0)):
		rect = pygame.Rect((0,0), self.rect.size)
		if self.disabled == 1:
			rect.move_ip(self.offs)
			rect.move_ip(self.offs)
			rect.move_ip(self.offs)
		elif self.click == 1:
			rect.move_ip(self.offs)
			rect.move_ip(self.offs)
		elif self.over == 1:
			rect.move_ip(self.offs)

		screen.blit(self.surf, offs, rect)
		
class wlives(wimage):
	def __init__(self, name, filename, lives = 0, dist=(20, 0), rect=None, z=0):
		wimage.__init__(self, name, filename, rect, z)
		self.dist = dist
		self.lives = lives
	
	def setLives(self, lives):
		self.lives = lives
		self.rect.width = self.surf.get_width + self.dist[0] * (lives - 1)
		self.rect.height = self.surf.get_height + self.dist[1] * (lives - 1)
		self.realign()
		
	def render(self, screen, offs = (0, 0)):
		for i in range(self.lives):
			screen.blit(self.surf, offs)
			offs = (offs[0] + dist[0], offs[1] + dist[1])
			
class whealth(window):
	def __init__(self, name, image1, image2 = None, max = 100, vert=0, rect=None, z=0):
		window.__init__(self, name, rect, z)
		self.max = max
		self.health = max
		self.setImage1(image1)
		self.setImage2(image2)
		self.rect.size = self.surf1.get_size()
		self.vert = vert
		
	def setImage1(self, image):
		try:
			if type(image) == type(''):
				self.surf1 = pygame.image.load(image).convert_alpha()
			else:
				self.surf1 = image
		except:
			raise Exception, "Image " + image + " failed to load."
		self.realign()
			
	def setImage2(self, image):
		if image is None:
			self.surf2 = None
			return
		try:
			if type(image) == type(''):
				self.surf2= pygame.image.load(image).convert_alpha()
			else:
				self.surf2 = image
		except:
			raise Exception, "Image " + image + " failed to load."
		self.realign()

	def render(self, screen, offs = (0,0)):
		if self.vert == 0:
			sourcerect = pygame.Rect(0,0, (self.surf1.get_width() / float(self.max)) * float(self.health), self.surf1.get_height())
		else:
			sourcerect = pygame.Rect(0,0, self.surf1.get_width(), (self.surf1.get_height() - (self.surf1.get_height() / float(self.max)) * float(self.health)))
		screen.blit(self.surf2, offs, sourcerect)
			
		if self.surf2 is not None:
			if self.vert == 0:
				destrect = pygame.Rect(sourcerect.width, 0, self.surf2.get_width() - sourcerect.width, self.surf2.get_height())
				destoffx = offs[0] + destrect.left;
				destoffs = (destoffx, offs[1])
			else:
				destrect = pygame.Rect(0, sourcerect.height, self.surf2.get_width(), self.surf2.get_height() - sourcerect.height)
				destoffy = offs[1] + destrect.top;
				destoffs = (offs[0], destoffy)
			screen.blit(self.surf1, destoffs, destrect)

class wspinner(wvartext):
	def incFunc(self, window, pos, button):
		self.dir = 1
		self.count = 1
	def uniFunc2(self, window, pos, button):
		self.dir = 0
		self.count = 0
	def decFunc(self, window, pos, button):
		self.dir = -1
		self.count = 1
		
	def __init__(self, name, inc_btn, dec_btn, obj, attr, amount=1, max=None, min=None, click = 30, rect=None, font=None, color=(255,255,255), alpha=255, z=0):
		wvartext.__init__(self, name, obj, attr, None, rect, font, color, alpha, z)
		self.amount = amount
		self.min = min
		self.max = max
		self.dir = 0
		self.count = 0
		self.click = 30
		inc_btn.function = self.incFunc
		inc_btn.function2 = self.uniFunc2
		dec_btn.function = self.decFunc
		dec_btn.function2 = self.uniFunc2
		
	def render(self, screen, offs=(0,0)):
		if self.dir != 0:
			if self.count < self.click and self.count != 1:
				self.count = self.count + 1
			else:
				self.count += 1
				value = getattr(self.obj, self.attr)
				value += self.dir * self.amount
				if self.max is not None and value > self.max:
					value = self.max
				if self.min is not None and value < self.min:
					value = self.min
				setattr(self.obj, self.attr, value)
				
		wtext.render(self, screen, offs)
		
class wspinnerset(window):
	def __init__(self, name, obj, attr, limage, rimage, text, amount=1, max=None, min=None, click=30, rect=None, font=None, color=(255,255,255), alpha=255, z=0):
		window.__init__(self, name, rect, z)
		
		self.text = wtext(name + "text", text, None, font, color, alpha)
		
		self.lbutton = wimgbutton(name + "lbutton", limage)
		self.rbutton = wimgbutton(name + "rbutton", rimage)
		self.rbutton.rect.left = self.rbutton.rect.width
		
		self.spinner = wspinner(name + "spinner", self.rbutton, self.lbutton, obj, attr, amount, max, min, click, None, font, color, alpha)
		self.spinner.rect.top = self.text.rect.bottom + 2
		self.lbutton.rect.top = self.spinner.rect.bottom + 2
		self.rbutton.rect.top = self.spinner.rect.bottom + 2
		self.spinner.rect.centerx = self.lbutton.rect.width
	
		self.add(self.text)
		self.add(self.lbutton)
		self.add(self.rbutton)
		self.add(self.spinner)
		
class wtxtbutton(window):
	def __init__(self, name, image, text, function=None, function2=None, font=None, color=(0,0,0), alpha=255, rect=None, z=0):
		window.__init__(self, name, rect, z)
		self.image = wimgbutton(name+"img", image, function, function2)
		self.text = wtext(name+"txt", text, None, font, color, alpha)
		self.add(self.image)
		self.image.add(self.text)
		self.text.align(ALIGN_MIDCENTER)
		
class wimagelabelbutton(window):
	def __init__(self, name, ximage, ximage2, function=None, function2=None, font=None, color=(0,0,0), alpha=255, rect=None, z=0):
		window.__init__(self, name, rect, z)
		self.image = wimgbutton(name+"img", ximage, function, function2)
		self.image2 = wimage(name+"label", ximage2)
		self.add(self.image)
		self.image.add(self.image2)
		self.image2.align(ALIGN_MIDCENTER)
		